<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{ active: active }">
      <span v-if="!changeResult.resultAddress" class="placeholder"
        >请选择配送地址</span
      >
      <span v-else class="value">{{ changeResult.resultAddress }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span
        @click="changeCity(area)"
        class="ellipsis"
        v-for="area in cityList"
        :key="area.code"
        >{{ area.name }}</span
      >
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { ref, reactive } from "vue";
export default {
  name: "xtxCity",
  setup() {
    // 3. 交互选择
    const changeResult = reactive({
      provinceCode: "", // 省code
      provinceName: "", // 省名称
      cityCode: "", // 城市code
      cityName: "", // 城市名称
      countyCode: "", // 地区code
      countyName: "", // 地区名
      resultAddress: "", // 最终结果地址
    });
    const loadCity = async () => {
      const res = await axios({
        method: "get",
        url: "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json",
      });
      console.log(res, 7766);
      cityList.value = res.data;
    };
    var cityList = ref([]);
    const active = ref(false);
    /* 开启对话框 */
    function start() {
      active.value = !active.value;
    }
    // 关闭对话框
    function close() {
      active.value = false;
    }
    const toggleDialog = () => {
      //1- 显示城市提示框
      /* 2-获取省市区 */
      start();
      loadCity();
    };
    // 3-根据省获取城市
    const changeCity = (area) => {
      // if (!area.areaList) return;
      if (area.level == 2) {
        changeResult.countyCode = area.code;
        changeResult.countyName = area.name;
        /* 获取完整地址 */
        changeResult.resultAddress =
          changeResult.provinceName +
          changeResult.cityName +
          changeResult.countyName;
        // 最后一层 关闭对话框
        close();
      }

      if (area.level == 0) {
        changeResult.provinceCode = area.code;
        changeResult.provinceName = area.name;
      }
      if (area.level == 1) {
        changeResult.cityCode = area.code;
        changeResult.cityName = area.name;
      }
      console.log(area, 44);
      cityList.value = area.areaList;
    };

    return { toggleDialog, active, cityList, changeCity, changeResult };
  },
};
</script>

<style lang="less" scoped>
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
